package org.cupk2.auth.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.cupk2.base.db.CupkDbFields;
import org.cupk2.base.domain.CupkUserContextLoader;
import org.cupk2.base.exception.CupkExceptionUtils;
import org.cupk2.auth.dao.DicTypeDao;
import org.cupk2.auth.domain.DicType;
import org.cupk2.auth.service.DicTypeServiceInter;
import org.cupk2.tools.db.DataQuery;
import org.cupk2.tools.db.DataResult;
import org.cupk2.tools.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 字典类型
 *
 */
@Service
@Slf4j
public class DicTypeServiceImpl implements DicTypeServiceInter {


    @Autowired
    private DicTypeDao dicTypeDao;

    @Transactional
    @Override
    public DicType insertDicTypeEntity(DicType dicType) {
        CupkDbFields.initInsertBean(dicType, CupkUserContextLoader.getCurrentUser());

        dicTypeDao.insert(dicType);


        return dicType;
    }

    @Transactional
    @Override
    public DicType editDicTypeEntity(DicType dicType) {
        DicType saveDicType = getDicTypeById(dicType.getId());
        CupkExceptionUtils.throwNullEx(saveDicType, I18n.msg("字典类型不存在:?", dicType.getId()));
        saveDicType.setState(dicType.getState());
        saveDicType.setNote(dicType.getNote());
        saveDicType.setName(dicType.getName());
        saveDicType.setKeyid(dicType.getKeyid());
        saveDicType.setSortcode(dicType.getSortcode());
        CupkDbFields.initUpdateBean(saveDicType, CupkUserContextLoader.getCurrentUser());
        dicTypeDao.update(saveDicType);
        return saveDicType;
    }

    @Transactional
    @Override
    public DicType getDicTypeById(String id) {
        return dicTypeDao.findById(id);
    }

    @Override
    public List<DicType> getDicTypes(DataQuery query) {
        return dicTypeDao.queryData(query.setCount(false)).getData(DicType.class);
    }


    @Transactional
    @Override
    public DataResult searchDicType(DataQuery query) {
        DataResult result = dicTypeDao.queryData(query);
        return result;
    }

    @Override
    public int getDicTypeNum(DataQuery query) {
        return dicTypeDao.countData(query);
    }


    @Transactional
    @Override
    public void delDicType(String id) {
        /*[tree：树形结构使用]
        if ( dicTypeDao.findByParentId(id).size() > 0) {
            throw new RuntimeException("不能删除该节点，请先删除其子节点");
        }
        */
        dicTypeDao.deleteById(id);
    }

    @Override
    public int getNum(DataQuery query) {
        return dicTypeDao.countData(query);
    }
    
    /*[tree：树形结构使用]
    @Transactional
    @Override
    public void moveTreeNode(List<String> sourceIds, String targetId) {
        for (String sourceId : sourceIds) {

            DicType node = getDicTypeById(sourceId);
            if (!"NONE".equals(targetId)) {
                DicType target = getDicTypeById(targetId);
                if (target.getTreecode().indexOf(node.getTreecode()) >= 0) {
                    throw new RuntimeException("不能够移动到其子节点下!");
                }
            }
            node.setParentid(targetId);
            dicTypeDao.update(node);

            List<DicType> list = dicTypeDao.findSubNodes(sourceId);
            for (DicType treenode : list) {
                initTreeCode(treenode.getId());
            }
        }
    }*/

    /**[tree：树形结构使用]
     * 构造treecode字段
     * @param treeNodeId
    private void initTreeCode(String treeNodeId) {
    DicType node = dicTypeDao.findById(treeNodeId);
    if (node.getParentid().equals("NONE")) {
    node.setTreecode(node.getId());
    } else {
    node.setTreecode(dicTypeDao.findById(node.getParentid()).getTreecode() + node.getId());
    }
    dicTypeDao.update(node);
    }
     */
    /* [tree：树形结构使用]
    @Transactional
    @Override
    public void autoSort(List<String> ids) {
        int sort = 0;
        for (String id : ids) {
            DicType node =  dicTypeDao.findById(id);
            if (sort == 0) {
                sort = node.getSortcode();
            }
            node.setSortcode(sort++);
            dicTypeDao.update(node);
        }
    }*/
}
